package com.offcn.sellergoods.service.impl;

import com.offcn.sellergoods.gruop.SpecGroup;
import com.offcn.sellergoods.mapper.SpecificationMapper;
import com.offcn.sellergoods.mapper.SpecificationOptionMapper;
import com.offcn.sellergoods.pojo.Specification;
import com.offcn.sellergoods.pojo.SpecificationOption;
import com.offcn.sellergoods.service.SpecificationService;
import com.offcn.common.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;

/****
 * @Author:ujiuye
 * @Description:Specification业务层接口实现类
 * @Date 2021/2/1 14:19
 *****/
@Service
public class SpecificationServiceImpl extends ServiceImpl<SpecificationMapper, Specification> implements SpecificationService {

    /**
     * 规格
     */
    @Autowired
    private SpecificationMapper specificationMapper;
    /**
     * 选项
     */
    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;

    /**
     * Specification条件+分页查询
     *
     * @param specification 查询条件
     * @param page          页码
     * @param size          页大小
     * @return 分页结果
     */
    @Override
    public PageResult<Specification> findPage(Specification specification, int page, int size) {
        Page<Specification> mypage = new Page<>(page, size);
        QueryWrapper<Specification> queryWrapper = this.createQueryWrapper(specification);
        IPage<Specification> iPage = this.page(mypage, queryWrapper);
        return new PageResult<Specification>(iPage.getTotal(), iPage.getRecords());
    }

    /**
     * Specification分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<Specification> findPage(int page, int size) {
        Page<Specification> mypage = new Page<>(page, size);
        IPage<Specification> iPage = this.page(mypage, new QueryWrapper<Specification>());

        return new PageResult<Specification>(iPage.getTotal(), iPage.getRecords());
    }

    /**
     * Specification条件查询
     * @param specification
     * @return
     */
    @Override
    public List<Specification> findList(Specification specification){
        //构建查询条件
        QueryWrapper<Specification> queryWrapper = this.createQueryWrapper(specification);
        //根据构建的条件查询数据
        return this.list(queryWrapper);
    }

    /**
     * Specification构建查询对象
     * @param specification
     * @return
     */
    public QueryWrapper<Specification> createQueryWrapper(Specification specification){
        QueryWrapper<Specification> queryWrapper = new QueryWrapper<>();
        if(specification!=null){
            // 主键
            if(!StringUtils.isEmpty(specification.getId())){
                queryWrapper.eq("id",specification.getId());
            }
            // 名称
            if(!StringUtils.isEmpty(specification.getSpecName())){
                queryWrapper.like("spec_name",specification.getSpecName());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        //先删外键
        QueryWrapper<SpecificationOption> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_id", id);
        specificationOptionMapper.delete(wrapper);
        //再删主键
        specificationMapper.deleteById(id);
    }

    /**
     * 修改Specification
     *
     * @param specGroup
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SpecGroup specGroup) {
        //规格
        Specification specification = specGroup.getSpecification();
        //选项
        List<SpecificationOption> specificationOptionList = specGroup.getSpecificationOptionList();

        //修改规格
        specificationMapper.updateById(specification);
        //改选项 ---先删除再添加
        Long id = specification.getId();
        QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spec_id", id);
        //删除
        specificationOptionMapper.delete(queryWrapper);

        for (SpecificationOption specificationOption : specificationOptionList) {
            specificationOption.setSpecId(id);
            //添加
            specificationOptionMapper.insert(specificationOption);
        }

    }

    /**
     * 增加Specification
     *
     * @param specGroup 保存两种数据：  规格和选项
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SpecGroup specGroup) {
        //先保存规格
        Specification specification = specGroup.getSpecification();
        specificationMapper.insert(specification);
        //获取规格生成的id
        Long id = specification.getId();

        //再保存选项
        List<SpecificationOption> optionList = specGroup.getSpecificationOptionList();
        for (SpecificationOption specificationOption : optionList) {
            specificationOption.setSpecId(id);
            specificationOptionMapper.insert(specificationOption);
        }
    }

    /**
     * 根据ID查询Specification
     *
     * @param id
     * @return
     */
    @Override
    public SpecGroup findById(Long id) {
        //规格
        Specification specification = specificationMapper.selectById(id);
        //选项
        QueryWrapper<SpecificationOption> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spec_id", id);
        List<SpecificationOption> specificationOptions = specificationOptionMapper.selectList(queryWrapper);

        SpecGroup specGroup = new SpecGroup();
        specGroup.setSpecification(specification);
        specGroup.setSpecificationOptionList(specificationOptions);
        return specGroup;
    }

    /**
     * 查询Specification全部数据
     *
     * @return
     */
    @Override
    public List<Specification> findAll() {
        return this.list(new QueryWrapper<Specification>());
    }

    /**
     * 查询规格下拉列表
     *
     * @return
     */
    @Override
    public List<Map> findOptions() {
        return specificationMapper.selectOpen();
    }
}
